//
//  SendMessageController.m
//  Coco_Music
//
//  Created by K。 on 15/7/3.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "SendMessageController.h"
#import <MessageUI/MessageUI.h>
#import <UIKit/UIKit.h>
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
@interface SendMessageController ()<UIAlertViewDelegate>

@end

@implementation SendMessageController
-(void)dealloc
{
    [_email release];
    [_content release];
    [super dealloc];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *zitiLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
    zitiLabel.font = [UIFont fontWithName:@"SXSLKF" size:22];
    zitiLabel.text = @"意见反馈";
    self.navigationItem.titleView = zitiLabel;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction:)];
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"home2"] style:UIBarButtonItemStyleDone target:self action:@selector(clickAction:)];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT * 0.5986, SCREEN_WIDTH * 0.281, 30)];
    [button setTitle:@"提交反馈" forState:UIControlStateNormal];
    button.font = [UIFont fontWithName:@"SXSLKF" size:20];
    button.tintColor = [UIColor blackColor];
    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 100;
    button.alpha = 0.8;
    [self.view addSubview:button];
   
    CALayer * downButtonLayer = [button layer];
    [downButtonLayer setMasksToBounds:YES];
    [downButtonLayer setCornerRadius:5.0];
    [downButtonLayer setBorderWidth:1.0];
    [downButtonLayer setBorderColor:[[UIColor grayColor] CGColor]];
    [self.view insertSubview:button atIndex:100];
    _content.delegate = self;
    _email.delegate = self;
}
- (void)buttonAction:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)clickAction:(UIButton *)button
{
    
    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = @"abcdefg@126.com";
    testMsg.toEmail = @"514389293@qq.com";
    testMsg.relayHost = @"smtp.126.com";
    testMsg.requiresAuth = YES;
    if (testMsg.requiresAuth) {
        testMsg.login = @"abcdefg@126.com";
        testMsg.pass = @"123456asd";
    }
    
    testMsg.wantsSecure = YES;
    testMsg.subject = @"反馈意见";
    testMsg.delegate = self;
    NSString *str = [_content.text stringByAppendingString:_email.text];
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                               str,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [testMsg send];
    });
    
    
}
- (void)messageSent:(SKPSMTPMessage *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"发送成功" message:@"感谢您的反馈" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert buttonTitleAtIndex:0];
    
    
}
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"发送失败" message:@"请在试一次" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert buttonTitleAtIndex:0];

    
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT * 0.165
                                                                   , SCREEN_WIDTH * 0.4, 30)];
        label.text = @"联系方式";
        label.font = [UIFont fontWithName:@"SXSLKF" size:20];
        label.textColor = [UIColor blackColor];
        [self.view addSubview:label];
        
        self.email = [[UITextField alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT * 0.23
                                                                   , SCREEN_WIDTH * 0.875, 30)];
        _email.borderStyle = UITextBorderStyleRoundedRect;
        _email.placeholder = @"邮箱/QQ/手机";
        [self.view addSubview:_email];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT * 0.306, SCREEN_WIDTH * 0.4, 30)];
        label2.text = @"反馈内容";
        label2.font = [UIFont fontWithName:@"SXSLKF" size:20];
        label2.textColor = [UIColor blackColor];
        [self.view addSubview:label2];
        
        self.content = [[UITextField alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT * 0.3767, SCREEN_WIDTH * 0.875, self.view.frame.size.height / 5)];
        //        [_content setBackgroundColor:[UIColor whiteColor]];
        _content.textAlignment = NSTextAlignmentLeft;
        _content.borderStyle = UITextBorderStyleRoundedRect;
        _content.backgroundColor=[UIColor clearColor];//设置文本框为透明
        
        [self.view addSubview:_content];
        
        [_email release];
        [_content release];
        
    }
    return self;
}

// 输入上移
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.view.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view endEditing:YES];
    
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = SCREEN_WIDTH;
    float height = SCREEN_HEIGHT;
    //上移n个单位，按实际情况设置
    CGRect rect=CGRectMake(0,-80,width,height);
    self.view.frame=rect;
    [UIView commitAnimations];
    return YES;
}


@end
